import numpy as np
import csv
import random
from datetime import datetime
from time import strftime

'''
Global and Local Empirical Bayes Smoothers with Gamma Model
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def readNeighbors(inputTXT):
    f = open(inputTXT,'r')
    line = f.readline()
    output = []
    while line:
        temp = line[:-1].split(',')
        output.append(temp)
        line = f.readline()
    f.close()
    return output
    

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def formateCAS(inputCAS):
    f = open(inputCAS,'r')
    line = f.readline()
    output = np.zeros((len(dataMatrix), 16))
    while line:
        temp = line[:-1].split(' ')
        tempid = FIPStoID[int(temp[0])]
        #output.append(temp)
        output[tempid, int(temp[-1]) - 1992] = int(temp[1])
        line = f.readline()
    f.close()
    return output
        
#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    unitCSV = 'C:/_DATA/migration89_08/COUNTY Migration/SaTScan/exclude_nearest/county.csv'
    dataMatrix = build_data_list(unitCSV)
    neighborTXT = 'C:/_DATA/migration89_08/COUNTY Migration/SaTScan/exclude_nearest/5p_neighbors_id.txt'
    neighbors = readNeighbors(neighborTXT)  #neighbor id = index in the county.csv
    #print neighbors[1]
    FIPStoID = {}
    i = 0
    for item in dataMatrix:
        FIPStoID[int(item[2])] = i
        i = i + 1

    period = [9293, 9394, 9495, 9596, 9697, 9798, 9899, 9900, 1, 102, 203, 304, 405, 506, 607, 708]
    year = range(1992, 2008)
    periodYear = {}
    i = 0
    for item in period:
        periodYear[str(item)] = year[i]
        i += 1

    inputCAS = 'C:/_DATA/migration89_08/COUNTY Migration/SaTScan/exclude_nearest/spacetime/spacetime_total_in_case_con.cas'
    case = formateCAS(inputCAS)
    #print case

    dataCSV = 'C:/_DATA/migration89_08/COUNTY Migration/SaTScan/exclude_nearest/total_in_FIPS.csv'
    percent = 50000
    
    row = -1
    count = 1
    f = open(dataCSV,'r')
    line = f.readline()
    while line:
        if row > -1:
            temp = line.split(',')
            if (int(temp[-2]) in FIPStoID) and (int(temp[-1]) in FIPStoID):
                iID = FIPStoID[int(temp[-2])]
                oID = FIPStoID[int(temp[-1])]
                if (iID <> oID):         
                    if(str(oID) in neighbors[iID]):
                        #tempyear = periodYear[temp[0]] - 1992
                        case[iID, periodYear[temp[0]] - 1992] -= int(temp[6])
        if int(row/percent) > 0:
            print count*percent
            row = 0
            count += 1
        row += 1
        line = f.readline()
    f.close()

    output = []
    i = 0
    for item in case:
        j = 0
        for c in item:
            output.append([dataMatrix[i, 2], c,j+1992])
            j += 1
        i += 1

    filePath = neighborTXT[:-4] + '_spacetime_results.cas'
    np.savetxt(filePath, output, delimiter=' ', fmt = '%i')
    
    print "end at " + getCurTime()
    print "==========================="

           
